﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DocumentFormat.OpenXml.Presentation;
using LSQL;

namespace Shopfloormanagement.PDCA
{
    public partial class PDCAview : DevExpress.XtraEditors.XtraForm
    {
        public static PDCAview instance = new PDCAview();
        public PDCAview()
        {
            instance = this;
            InitializeComponent();
        }

        public static int currP_id = 0;
        public static int currPDCAid = 0;

        #region Load information
        private void Load_Pinfo()
        {
            DataTable dtResP = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_P", $"ID = {currP_id}");
            if (dtResP.Rows.Count > 0)
            {
                txtRes.Text = dtResP.Rows[0]["Responsible"].ToString();
                txtDate.Text = dtResP.Rows[0]["ComplainDate"].ToString();

                txtProblem.Text = dtResP.Rows[0]["Description"].ToString();
                txtRoot.Text = dtResP.Rows[0]["Rootcause"].ToString();

                picProblem.Image = publicFunc.Base64ToImage(dtResP.Rows[0]["ProblemPic"].ToString());
                DataTable dtPDCA = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_view", $"ID = {dtResP.Rows[0]["PDCA_id"]}");
                int type = (int)dtPDCA.Rows[0]["ProblemType_id"];
                var obj = Properties.Resources.ResourceManager.GetObject($"_{type}quality");
                if (obj is Image img)
                {
                    pic_P_type.Image = img;

                }

            }
        }

        private string Dstep_id = string.Empty;
        private void Load_Dinfo()
        {
            DataTable dtResD_short = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_D", $"P_id = {currP_id} and ActionType = 'short'");
            DataTable dtResD_long = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_D", $"P_id = {currP_id} and ActionType = 'long'");

            int[] idShort = new int[dtResD_short.Rows.Count];
            int[] idLong = new int[dtResD_long.Rows.Count];
            for (int i = 0; i < dtResD_short.Rows.Count; i++)
            {
                idShort[i] = (int)dtResD_short.Rows[i]["ID"];
            }
            for (int i = 0; i < dtResD_long.Rows.Count; i++)
            {
                idLong[i] = (int)dtResD_long.Rows[i]["ID"];
            }
            Dstep_id = string.Join(",", idShort) + "," + string.Join(",", idLong);

            gcShort.DataSource = dtResD_short;
            gcLong.DataSource = dtResD_long;
            if (dtResD_short.Rows.Count < 1)
            {
                return;
            }
            if (dtResD_long.Rows.Count < 1)
            {
                return;
            }


            gvShort.Columns["ID"].Visible = false;
            gvShort.Columns["P_id"].Visible = false;
            gvShort.Columns["ActionType"].Visible = false;

            gvLong.Columns["ID"].Visible = false;
            gvLong.Columns["P_id"].Visible = false;
            gvLong.Columns["ActionType"].Visible = false;

            gvShort.Columns["Action"].Width = 600;
            gvShort.Columns["Who"].Width = 100;
            gvShort.Columns["When"].Width = 100;
            gvShort.Columns["Status"].Width = 74;

            gvLong.Columns["Action"].Width = 600;
            gvLong.Columns["Who"].Width = 100;
            gvLong.Columns["When"].Width = 100;
            gvLong.Columns["Status"].Width = 74;


        }

        private void Load_Cinfo()
        {
            DataTable dtRes_C = SQL.ReturnDataTable3(SystemInfo.SQL, "View_PDCA_C", $"P_id = {currP_id}");
            if (dtRes_C.Rows.Count < 1)
            {
                return;
            }
            gcCheck.DataSource = dtRes_C;


            gvCheck.Columns["P_id"].Visible = false;

        }

        private void Load_Ainfo()
        {
            DataTable dtRes_A = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_A", $"PDCA_id = {currPDCAid}");
            if (dtRes_A.Rows.Count < 1)
            {
                return;
            }
            gcAct.DataSource = dtRes_A;

            gvAct.Columns["ID"].Visible = false;
            gvAct.Columns["PDCA_id"].Visible = false;
        }
        #endregion

        private void GridView_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "Status")
            {
                switch (Convert.ToInt32(e.Value))
                {
                    case 1: e.DisplayText = "0%"; break;
                    case 2: e.DisplayText = "25%"; break;
                    case 3: e.DisplayText = "50%"; break;
                    case 4: e.DisplayText = "75%"; break;
                    case 5: e.DisplayText = "100%"; break;
                }
            }
        }

        private void PDCAview_Load(object sender, EventArgs e)
        {
            Load_Pinfo();
            Load_Dinfo();
            Load_Cinfo();
            Load_Ainfo();

            this.gvShort.CustomColumnDisplayText += GridView_CustomColumnDisplayText;
            this.gvLong.CustomColumnDisplayText += GridView_CustomColumnDisplayText;
            this.gvAct.CustomColumnDisplayText += GridView_CustomColumnDisplayText;
            this.gvCheck.CustomColumnDisplayText += GridView_CustomColumnDisplayText;
        }

        private void pictureBox3_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string msg = "";

            foreach (DevExpress.XtraGrid.Columns.GridColumn col in gvShort.VisibleColumns)
            {
                msg += string.Format("{0}: {1}px\n", col.Caption, col.Width);
            }
            MessageBox.Show(msg, "可见列列宽");
        }
    }
}